package com.ylp.sys.controller;


import com.ylp.common.response.Result;
import com.ylp.sys.domain.dto.GeneratorDatasourceDto;
import com.ylp.sys.domain.entity.GeneratorDatasource;
import com.ylp.sys.service.GeneratorDatasourceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cc
 * @since 2024-07-30
 */
@Tag(name = "代码生成接口")
@RestController
@RequestMapping("/generatorDatasource")
public class GeneratorDatasourceController {
    private final GeneratorDatasourceService generatorDatasourceService;

    public GeneratorDatasourceController(GeneratorDatasourceService generatorDatasourceService) {
        this.generatorDatasourceService = generatorDatasourceService;
    }

    @Operation(summary = "查询列表")
    @GetMapping("/getList")
    public Result<List<GeneratorDatasource>> getList() {
        return Result.success(generatorDatasourceService.list());
    }

    @Operation(summary = "删除用户")
    @PostMapping("/delete")
    public Result<String> delete(@RequestBody List<Long> ids) {
        try {
            generatorDatasourceService.removeBatchByIds(ids);
        } catch (Exception e) {
            return Result.error();
        }
        return Result.success();
    }

    @Operation(summary = "修改")
    @PutMapping("/edit")
    public Result<String> edit(@RequestBody GeneratorDatasource generatorDatasource) {
        try {
            generatorDatasourceService.updateById(generatorDatasource);
        } catch (Exception e) {
            return Result.error();
        }
        return Result.success();
    }

    @Operation(summary = "添加")
    @PostMapping("/add")
    public Result<String> add(@RequestBody GeneratorDatasource generatorDatasource) {
        try {
            generatorDatasourceService.save(generatorDatasource);
        } catch (Exception e) {
            return Result.error();
        }
        return Result.success();
    }

    @Operation(summary = "根据id查询")
    @GetMapping("/getById")
    public Result<GeneratorDatasource> getById(@RequestParam Long id) {
        try {
            GeneratorDatasource generatorDatasource = generatorDatasourceService.getById(id);
            return Result.success(generatorDatasource);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    @Operation(summary = "代码生成")
    @PostMapping("/generator")
    public Result<String> generator(@RequestBody GeneratorDatasourceDto datasourceDto) {
        try {
            generatorDatasourceService.generator(datasourceDto);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("代码生成失败, message:" + e.getMessage());
        }
        return Result.success("代码生成成功");
    }










}
